/**
 * Created by szc on 2016/12/29.
 */

class CrystalPanle extends egret.DisplayObjectContainer {

    public context;

    private paylistBtn: PaylistBtn;

    public rotatBtn: RotationBtn;

    private all_win_panle;
    public all_zhu_panle:CrystalDisS;

    public autoRotationBtn: AutoRotation;

    public isAuto:boolean = false;

    public cur_zhu:CrystalDisB;
    public cur_multiple:CrystalDisB;

    public autoRotationIndex = 0;
    public rotationNumArray = [5, 25, 50, 100, 200];



    // private AutoRotation;

    public constructor(context: any) {
        super();
        this.context = context;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        this.setDefalutPanle();
        // this.AutoRotation = this.getAutoRotation();
    }
    /**
     * 设置默认布局页面
     */
    public setDefalutPanle(): any {
        //加入水晶板控制
        var crystalPanle = new egret.DisplayObjectContainer();
        var wpercentage = 0.88;
        var hpercentage = 0.94;
        // 首先加入的底板
        // var imgCon = [];
        // imgCon['url'] = 'null_png';
        // imgCon['width'] = this.stage.stageWidth * wpercentage;
        // imgCon['height'] = this.stage.stageHeight * 0.15;
        // crystalPanle.addChild(new ImageBitmap(imgCon).getImgPoint());

        //设置赔付表接口
        // this.paylistBtn = new PaylistBtn();
        // this.paylistBtn.width = 120;
        // this.paylistBtn.height = 80;
        // this.paylistBtn.x = 55;
        // // this.paylistBtn.y = 0;
        // crystalPanle.addChild(this.paylistBtn);
        // _btn3.setdisState();



        //设置注的画面
        var arr = [0.01, 0.02, 0.05, 0.1, 0.5, 1, 5, 10];
        var zhu_panle = new CrystalDisB("line_bet_top_png", crystalPanle, arr, this);
        this.cur_zhu = zhu_panle;
        zhu_panle.x = 150;
        zhu_panle.y = 10;
        crystalPanle.addChild(zhu_panle);
        this.cur_zhu.alpha = 0;


        //设置投注数的画面
        var arr = [25, 50, 75, 100, 125];
        var lottery_multiple_panle = new CrystalDisB("line_number_top_png", crystalPanle, arr, this);
        this.cur_multiple = lottery_multiple_panle;
        lottery_multiple_panle.x = 10;
        lottery_multiple_panle.y = 10;
        crystalPanle.addChild(lottery_multiple_panle);
        this.cur_multiple.alpha = 0;


        //设置总下注的画面
        this.all_zhu_panle = new CrystalDisS("all_bet_top_png", crystalPanle,this);
        this.all_zhu_panle.anchorOffsetX = this.all_zhu_panle.width/2;
        this.all_zhu_panle.anchorOffsetY = this.all_zhu_panle.height/2;
        this.all_zhu_panle.x = 400;
        this.all_zhu_panle.y = 62;
        crystalPanle.addChild(this.all_zhu_panle);


        //设置总赢得的图像
        this.all_win_panle = new CrystalDisS("this_monty_top_png", crystalPanle,this);
        this.all_win_panle.x = 550;
        this.all_win_panle.y = 12;
        crystalPanle.addChild(this.all_win_panle);



        //设置水晶板的位置
        this.x = this.stage.stageWidth * (1 - wpercentage) / 2;
        this.y = this.stage.stageHeight * 0.85;
        this.addChild(crystalPanle);

        
        //设置旋转
        this.rotatBtn = new RotationBtn(this.context);
        this.rotatBtn.anchorOffsetX = this.rotatBtn.width/2;
        this.rotatBtn.anchorOffsetY = this.rotatBtn.height/2;
        this.rotatBtn.y = -70 + 85;
        this.rotatBtn.x = 800 + 85;
        this.rotatBtn.scaleX = 0.6;
        this.rotatBtn.scaleY = 0.6;
        this.addChild(this.rotatBtn);



        //设置自动旋转
        this.autoRotationBtn = new AutoRotation(this.rotationNumArray[this.autoRotationIndex],this.context);
        this.autoRotationBtn.anchorOffsetX = this.autoRotationBtn.width/2;
        this.autoRotationBtn.anchorOffsetY = this.autoRotationBtn.height/2;
        this.autoRotationBtn.x = 960;
        this.autoRotationBtn.y = -85;
        this.autoRotationBtn.scaleX = 0.6;
        this.autoRotationBtn.scaleY = 0.6;
        this.addChild(this.autoRotationBtn);
        return crystalPanle;
    }

    public setUserPanle() {
        this.isAuto = false;
        //设置赔付表接口
        // this.paylistBtn.setAvbleState();
        //设置旋转
        this.rotatBtn.setAvbleState();
        //设置停止自动旋转
        this.autoRotationBtn.visible = false;
        //this.autoRotationBtn.setAutoReset(this.rotationNumArray[this.autoRotationIndex]);
        this.cur_multiple.setEditabledState();
        this.cur_zhu.setEditabledState();
        this.context.moreBtn.touchEnabled = true;
        this.context.crystalPanle.all_zhu_panle.touchEnabled = true;

    }
    public setDisPanle(): any {
        //设置赔付表接口
        // this.paylistBtn.setDisState();
        //设置旋转
        if (this.context.gunDeal.needGun > 0) {
            //设置剩余的字体数量
            this.autoRotationBtn.setNumber(this.context.gunDeal.needGun - 1);
            this.context.moreBtn.moreUI.setAutoStopNum(this.context.gunDeal.needGun - 1);

        } else {
            this.rotatBtn.setDisState();
        }
        //设置停止自动旋转
        if (!this.context.freePanle.ishasFree()) {
            this.autoRotationBtn.setStopAvbleState();
        } else {
            this.autoRotationBtn.setStopDisState();
        }
        this.cur_multiple.setDisState();
        this.cur_zhu.setDisState();
    }

    /**
     * 改变当前的注面板，在自动旋转动画的时候的可以编辑当前的住
     */
    public changezhu() {
        // alert();
        if (this.context.gunDeal.needGun > -1) return;
        this.cur_multiple.setEditabledState();
        this.cur_zhu.setEditabledState();
    }

    /**
     * 计算投注数
     */
    public ca_zhu(): void {
        var zhu = this.getCuZhu() * this.getCuMultiple()*25;
        this.all_zhu_panle.setNum(zhu);
        this.set_win(0);
    }

    public set_zhu_win(zhu: number, win: number): void {
        this.all_win_panle.setNum(win);
        this.all_zhu_panle.setNum(zhu);
    }

    public set_win(win: number): void {
        this.all_win_panle.setNum(win);
    }

    /**
     * 设置需要旋转的次数
     */
    private setRotationNum(evt: egret.TouchEvent): void {
        var num = evt.target.num;
        this.context.gunDeal.gunNumRotate(num);
    }

    public setRotationNumNoEvent(num:number)
    {
        this.autoRotationBtn.setNumber(num);
        this.context.gunDeal.gunNumRotate(num);
    }

    private getAutoRotation(): any {
        /**
         * 控制自动旋转
         * @type {egret.DisplayObjectContainer}
         */
        var auto_group = new egret.DisplayObjectContainer();
        var _btn = new eui.Button();
        _btn.skinName = "m_rotation_10";
        _btn.width = this.stage.stageWidth / 10;
        _btn.height = _btn.width * 13 / 29;
        _btn.y = 0;
        _btn.touchEnabled = true;
        _btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setRotationNum, this);
        _btn['num'] = 10;
        auto_group.addChild(_btn);

        var _btn = new eui.Button();
        _btn.skinName = "m_rotation_30";
        _btn.width = this.stage.stageWidth / 10;
        _btn.height = _btn.width * 13 / 29;
        _btn.y = _btn.height;
        _btn.touchEnabled = true;
        _btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setRotationNum, this);
        _btn['num'] = 30;
        auto_group.addChild(_btn);


        var _btn = new eui.Button();
        _btn.skinName = "m_rotation_50";
        _btn.width = this.stage.stageWidth / 10;
        _btn.height = _btn.width * 13 / 29;
        _btn.y = 2 * _btn.height;
        _btn.touchEnabled = true;
        _btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setRotationNum, this);
        _btn['num'] = 50;
        auto_group.addChild(_btn);


        var _btn = new eui.Button();
        _btn.skinName = "m_rotation_99";
        _btn.width = this.stage.stageWidth / 10;
        _btn.height = _btn.width * 13 / 29;
        _btn.y = 3 * _btn.height;
        _btn.touchEnabled = true;
        _btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.setRotationNum, this);
        _btn['num'] = 99;
        auto_group.addChild(_btn);

        var _btn = new eui.Button();
        _btn.skinName = "m_rotation_stop";
        _btn.width = this.stage.stageWidth / 10;
        _btn.height = _btn.width * 85 / 203;
        _btn.y = 4 * _btn.height + 10;
        _btn.touchEnabled = true;
        _btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.openAutoSysPanle, this);
        auto_group.addChild(_btn);
        return auto_group;
    }

    /**
     *
     */
    private openAutoSysPanle(): void {
        // this.setAutoRotation();
        // this.context.openNewchildPanle("unit_stop");

    }

    /**
     * 设置自动旋转次数打开的界面
     */
    public setAutoRotation(): void {
        // if (this.context.contains(this.AutoRotation)) {
        //     this.context.removeChild(this.AutoRotation);
        // } else {
        //     this.context.addChild(this.AutoRotation);
        //     this.AutoRotation.y = 0;
        //     this.AutoRotation.x = 0;
        //     this.AutoRotation.y = this.y - 260;
        //     this.AutoRotation.x = this.x + 800;
        // }

        // this.autoRotationBtn.setAutoDisState();
        this.context.gunDeal.gunNumRotate(this.rotationNumArray[this.autoRotationIndex]);
        this.context.crystalPanle.rotatBtn.setAutoStopState();
        this.isAuto = true;
    }
    public isAutoRotation() {
        // if (this.context.contains(this.AutoRotation)) {
        //     return true;
        // } else {
        //     return false;
        // }

        return this.isAuto;
    }

    /**
     * setAutoRotateDisplay
     */
    public setAutoRotateDisplay() {
//      console.log("At");
        this.autoRotationBtn.setAutoDisState();
        this.rotatBtn.setDisState();
    }

    public setStopRotateDisplay() {
//      console.log("stop");
        this.autoRotationBtn.visible = false;
        // this.rotatBtn.setDisState();
    }

    /**
     * 关闭自动旋转
     */
    public closeAutoRotation(): void {
        // if (this.context.contains(this.AutoRotation)) {
        //     this.context.removeChild(this.AutoRotation);
        // }
        
        this.isAuto = false;
    }
    public getCuZhu(): any {
        return this.cur_zhu.getNum();
    }

    public getCuMultiple(): any {
        return this.cur_multiple.getNum();
    }

    public displayUserPanle(): void {
        this.setUserPanle();
    }

    public displayDisPanle(): void {
        this.setDisPanle();
    }
}